$(function () {
    $("#jqGrid").Grid({
        url: '../sportsuser/list',
        colModel: [
			{label: 'id', name: 'did', index: 'did', key: true, hidden: true},
			{label: '昵称', name: 'nickname', index: 'nickname', width: 80,align:'center'},
			{label: '用户头像', name: 'img_url', index: 'img_url', width: 80,
				formatter:function (value) {
					return transImg(value);
				},align:'center'
			},
			{label: '手机号码', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '生日 ', name: 'birthday', index: 'birthday', width: 80,align:'center'},
			{label: '性别', name: 'gender', index: 'gender', width: 80,
				formatter:function (value) {
					var temp = "";
					if (value == 0) {
						temp = "<span class=\"label label-success\">男</span>";
					} else if (value == 1) {
						temp = "<span class=\"label label-danger\">女</span>";
					}
					return temp;
				},align:'center'
			},
            {label: '是否锁定', name: 'islock', index: 'islock', width: 80,
                formatter:function (value) {
                    var temp = "";
                    if (value == 0) {
                        temp = "<span class=\"label label-success\">未锁定</span>";
                    } else if (value == 1) {
                        temp = "<span class=\"label label-danger\">已锁定</span>";
                    }
                    return temp;
                },align:'center'
            },
			{label: '可用DSPC', name: 'dspc', index: 'dspc', width: 80,align:'center'},
			{label: '冻结DSPC', name: 'lockdspc', index: 'lockdspc', width: 80,align:'center'},
			{label: 'DSPC总量', name: 'totalDspc', index: 'totalDspc', width: 80,align:'center'},
			{label: '用户住址', name: 'address', index: 'address', width: 80,hidden:true}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
		showUpdate:true,
        title: null,
		sportsUser: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
			updateDspc:0,
			updateLockDspc:0,
			id:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.showUpdate = true;
			vm.title = "新增";
			vm.sportsUser = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.showUpdate = true;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.sportsUser.did == null ? "../sportsuser/save" : "../sportsuser/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.sportsUser),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../sportsuser/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../sportsuser/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.sportsUser = r.sportsUser;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showUpdate = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
		handleSuccess: function (res, file) {
			vm.sportsUser.img_url = file.response.url;
		},
		handleFormatError: function (file) {
			this.$Notice.warning({
				title: '文件格式不正确',
				desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
			});
		},handleMaxSize: function (file) {
			this.$Notice.warning({
				title: '超出文件大小限制',
				desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
			});
		},eyeImagePicUrl: function () {
			var url = vm.sportsUser.img_url;
			eyeImage(url);
		},exportPort:function (event) {
			let ids = getSelectedRows("#jqGrid");
			if(ids!=null){
				var exportUrl = "../sportsuser/userexportIds?ids="+ids;
				location.href = exportUrl;
			}else{
				var exportUrl = "../sportsuser/userexport?1=1";
				if(vm.q.name!=''){
					exportUrl += "&name="+vm.q.name;
				}
				location.href = exportUrl;
			}

		},updateDspcCard:function (event) {
			let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.q.id = id;
			vm.showList = true;
			vm.showUpdate = false;
		},
		updateDspc:function () {
			var json = "{\"id\":\""+vm.q.id+"\",\"dspc\":\""+vm.q.updateDspc+"\",\"lockdspc\":\""+vm.q.updateLockDspc+"\"}";
			console.log(json);
			confirm('是否确认修改该用户Dspc数量？', function () {
				Ajax.request({
					url: "../sportsuser/updateDspc",
					params: JSON.stringify(vm.q),
					type: "POST",
					contentType: "application/json",
					successCallback: function () {
						alert('操作成功', function (index) {
							vm.reload();
						});
					}
				});
			});
		}
	}
});